package com.nengchuan.eic.nceconomy.service;

import com.eic.review.sdk.vo.IndividualProjectInfoVo;
import com.eic.review.sdk.vo.ProjectInfoVo;
import com.github.pagehelper.PageInfo;
import com.nengchuan.eic.nceconomy.constant.CommonConstant;
import com.nengchuan.eic.nceconomy.constant.ProjectEnum;
import com.nengchuan.eic.nceconomy.dao.ExcelFileMapper;
import com.nengchuan.eic.nceconomy.entity.dto.ProjectDTO;
import com.nengchuan.eic.nceconomy.entity.po.ExcelFile;
import com.nengchuan.eic.nceconomy.entity.vo.IndividualProjectInfoVO;
import com.nengchuan.eic.nceconomy.entity.vo.ProjectVO;
import com.nengchuan.eic.nceconomy.fegin.ProcessFeignFileService;
import com.nengchuan.eic.nceconomy.fegin.ProcessFeignTechnicalCassicsService;
import com.nengchuan.model.model.WrappedResult;
import com.nsc.boot.common.sdk.msg.ApiResult;
import com.nsc.boot.common.sdk.vo.IPage;
import com.nsc.boot.common.utils.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述:
 *
 * @Author: liurui
 * @Date: 2020/4/26 0026 上午 10:53
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProcessFeignTechnicalCassicsService processFeignTechnicalCassicsService;


    @Autowired
    private ExcelFileMapper excelFileMapper;

    /**
     * 查询输变电工程子工程
     *
     * @param projectId 输变电工程id
     * @return 子项目list
     */
    @Override
    public WrappedResult<List<IndividualProjectInfoVO>> getDetailList(Long projectId) {
        List<IndividualProjectInfoVO> list=new ArrayList<>();
        ApiResult<List<IndividualProjectInfoVo>> individualProjectInfoVo =
                processFeignTechnicalCassicsService.getIndividualProjectInfoVo(projectId);
        if(individualProjectInfoVo==null){
            return WrappedResult.fail(CommonConstant.responseCode.ERROR, "获取列表失败");
        }
        if (CommonConstant.responseCode.SUCCESS == individualProjectInfoVo.getCode()) {
            if(individualProjectInfoVo.getData()!=null){
                individualProjectInfoVo.getData().forEach(x->{
                    IndividualProjectInfoVO vo=new IndividualProjectInfoVO();
                    vo.generl(x);
                    list.add(vo);
                });
            }
            return WrappedResult.success(list);
        } else {
            return WrappedResult.fail(individualProjectInfoVo.getCode(), individualProjectInfoVo.getMsg());
        }
    }

    /**
     * 分页查询
     *
     * @param projectDTO
     * @return
     */
    @Override
    public ProjectVO getList(ProjectDTO projectDTO) {
        ProjectVO projectVO=new ProjectVO();
        ApiResult<IPage<ProjectInfoVo>> projectInfo = processFeignTechnicalCassicsService.getProjectInfo(projectDTO.getSize(), projectDTO.getCurrent(), projectDTO.getProjectStage(), projectDTO.getVoltageGrade());
        if(projectInfo!=null&&CommonConstant.responseCode.SUCCESS==projectInfo.getCode()){
            IPage<ProjectInfoVo> data = projectInfo.getData();
            List<Integer> project = data.getRecords().stream().map(ProjectInfoVo::getProjectStage).distinct().collect(Collectors.toList());
            List<String> voltage = data.getRecords().stream().map(ProjectInfoVo::getVoltageGrade).distinct().collect(Collectors.toList());
            projectVO.setProjectStage(project);
            projectVO.setVoltageGrade(voltage);
            projectVO.setTotalSize(data.getTotal());
            projectVO.setProjectInfoVo(data.getRecords());
        }
        return projectVO;
    }

    /**
     * 根据项目查询本库的工程类型
     *
     * @param projectId
     * @return
     */
    @Override
    public String getProjectType(Long projectId,Long pid,String categoryIndex) {
        return excelFileMapper.getProjectTypeByProjectId(projectId,pid,categoryIndex);
    }
}
